package com.qf.fmw.controller;

import com.qf.fmw.entity.vo.RespResult;
import com.qf.fmw.service.GoodsTypeService;
import com.qf.fmw.service.impl.GoodsTypeServiceImpl;
import com.qf.fmw.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/GoodsTypeController")
public class GoodsTypeController extends BaseServlet {
    protected GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();

    //显示商品分类
    protected RespResult showGoodsTypes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return goodsTypeService.showAllGoodsTypes();
    }

    //搜索商品分类
    protected RespResult searchGoodsTypes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String level = request.getParameter("level");
        String name = request.getParameter("name");
        return goodsTypeService.searchGoodsTypes(level, name);
    }

    //搜索商品分类 (按等级)
    protected RespResult searchGoodsTypesByLevel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String level = request.getParameter("level");
        return goodsTypeService.searchGoodsTypesByLevel(level);
    }

    //搜索商品分类 (按名称)
    protected RespResult searchGoodsTypesByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        return goodsTypeService.searchGoodsTypesByName(name);
    }

    // 修改商品分类
    protected RespResult updateGoodsClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        String name = request.getParameter("name");
        String level = request.getParameter("level");
        String belongType = request.getParameter("belongType");

        return goodsTypeService.updateGoodsType(typeId, name, level, belongType);
    }

    // 删除商品分类
    protected RespResult deleteGoodsClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        return goodsTypeService.deleteGoodsType(typeId);
    }

    //显示商品
    protected RespResult showGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return goodsTypeService.showAllGoods();
    }

    //搜索商品
    protected RespResult searchGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pubdate = request.getParameter("pubdate");
        return goodsTypeService.searchGoods(name, pubdate);
    }

    // 删除商品
    protected RespResult deleteGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pId = Integer.parseInt(request.getParameter("pId"));
        return goodsTypeService.deleteGoods(pId);
    }

    // 更新商品
    protected RespResult updateGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pId = Integer.parseInt(request.getParameter("pId"));
        String pName = request.getParameter("pName");
        String pPrice = request.getParameter("pPrice");
        String pTime = request.getParameter("pTime");
        String pInfo = request.getParameter("pInfo");
        return goodsTypeService.updateGoods(pId, pName, pPrice, pTime, pInfo);
    }

    //描述(点击描述查看描述内容)
    protected RespResult getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pId = Integer.parseInt(request.getParameter("pId"));
        return goodsTypeService.getGoodsInfo(pId);
    }

}
